from Case.python_ui.Common.basepage import Basepage
import pytest, time, datetime
from Case.python_ui.Pageobject.system_setting_page import SystemSettingPage
from Case.python_ui.Pageobject.login_pages import Loginpages
from Case.python_ui.Test_data.login_data import Logindata
from selenium import webdriver
from Case.python_ui.PageLocate.user_product_provider_locator import ProductProviderLocator
from selenium.common.exceptions import NoSuchElementException
from Case.python_ui.PageLocate.user_my_resource_locator import UserMyResourceLocator
import os
import xlrd

class UserProductProvider(Basepage):

    # 判断文件存在某字段
    def excel_element_is_exist(self, locate, list_field_value):
        doc = '判断表格字段是否存在'
        excel_path = xlrd.open_workbook(locate)
        excel_sheet = excel_path.sheet_by_index(0)
        # 读取文件
        list_fileds = excel_sheet.row_values(0)
        count = 0
        for list_filed in list_fileds:
            if list_filed != list_field_value:
                count += 1
            else:
                assert True
                print("文件包含字段：",list_field_value)
        # 获取该列数据
        excel_provider = excel_path.getExcelData(list_field_value)
        print(excel_provider)

    def check_excel_overview_provider(self, file_name_value):
        time.sleep(2)
        # 验证文件中新增provider
        # path = r"C:\Users\25294\Downloads"
        path = '/home/seluser/Downloads/'
        files = os.listdir(path)
        for file in files:
            if file[0:20] == file_name_value:
                excel_path_vlalue = (f'{path}{file}')
                print(excel_path_vlalue)
                self.excel_element_is_exist(excel_path_vlalue, 'Provider')
                os.remove(excel_path_vlalue)
                break
            else:
                assert False,"文件不存在"

    def check_excel_detail_provider(self, file_name_value):
        time.sleep(2)
        # 验证文件中新增provider
        # path = r"C:\Users\25294\Downloads"
        path = '/home/seluser/Downloads/'
        files = os.listdir(path)
        for file in files:
            if file[0:28] == file_name_value:
                excel_path_vlalue = (f'{path}/{file}')
                print(excel_path_vlalue)
                self.excel_element_is_exist(excel_path_vlalue, 'Provider')
                os.remove(excel_path_vlalue)
                break
            else:
                assert False,"文件不存在"

    # 进入Spending Summary界面
    def enter_bills_pages(self):
        time.sleep(2)
        doc = "进入Spending Summary界面"
        for i in range(3):
            try:
                eles = self.get_elements(ProductProviderLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(ProductProviderLocator.bills_tab, doc)
                self.click_element(ProductProviderLocator.bills_tab, doc)
                self.wait_elevisble(ProductProviderLocator.bills_spending_summary_title_btn, doc)
                self.click_element(ProductProviderLocator.bills_spending_summary_title_btn, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(ProductProviderLocator.bills_spending_summary_page, doc)

    # spending summary页面新增provider字段
    def spending_summary_add_provider(self):
        time.sleep(2)
        doc = "spending summary页面新增provider字段"
        date_time = (datetime.date.today()).strftime('%Y%m%d')
        # overview页面有产品名称提供者弹窗
        self.move_mouse_to_element(ProductProviderLocator.spending_summary_overview_product_name)
        time.sleep(1)
        self.check_attribute(self.get_element(ProductProviderLocator.spending_summary_overview_product_box, doc).text, 'Provider: Flou')
        # # 点击下载按钮
        self.move_mouse_to_click_element(ProductProviderLocator.spending_summary_overview_download_button, doc)
        time.sleep(10)
        overview_file_name = 'CostSummary_'+date_time
        self.check_excel_overview_provider(overview_file_name)
        # 点击detail 进入detail页面
        self.move_mouse_to_click_element(ProductProviderLocator.spending_summary_detail_tab, doc)
        time.sleep(2)
        self.wait_elevisble(ProductProviderLocator.spending_summary_detail_product_name, doc)
        self.move_mouse_to_element(ProductProviderLocator.spending_summary_detail_product_name)
        time.sleep(1)
        self.check_attribute(self.get_element(ProductProviderLocator.spending_summary_detail_product_box, doc).text, 'Provider: Flou')
        # 点击下载按钮
        self.move_mouse_to_click_element(ProductProviderLocator.spending_summary_detail_download_button, doc)
        time.sleep(10)
        detail_file_name = 'ProductBillingCycle_'+date_time
        self.check_excel_detail_provider(detail_file_name)


    # 进入Orders界面
    def enter_orders_pages(self):
        time.sleep(2)
        doc = "进入Orders界面"
        for i in range(3):
            try:
                eles = self.get_elements(ProductProviderLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(ProductProviderLocator.order_management_tab, doc)
                self.click_element(ProductProviderLocator.order_management_tab, doc)
                self.wait_elevisble(ProductProviderLocator.orders_title_btn, doc)
                self.click_element(ProductProviderLocator.orders_title_btn, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(ProductProviderLocator.orders_page, doc)

    # Orders页面新增provider字段
    def orders_add_provider(self):
        time.sleep(2)
        doc = "Orders页面新增provider字段"
        # 页面有产品名称提供者弹窗
        self.move_mouse_to_element(ProductProviderLocator.orders_product_name)
        time.sleep(1)
        self.check_attribute(self.get_element(ProductProviderLocator.orders_product_box, doc).text, 'Provider: Flou cloud')
        # 点击order id 进入detail页面
        self.move_mouse_to_click_element(ProductProviderLocator.orders_product_order_id, doc)
        time.sleep(2)
        self.wait_elevisble(ProductProviderLocator.orders_detail_page, doc)
        time.sleep(1)
        self.check_attribute(self.get_element(ProductProviderLocator.orders_detail_provider_title, doc).text, 'Provider')

    # 进入Resource Packages页面
    def enter_resource_packages_pages(self):
        time.sleep(2)
        doc = "进入Resource Packages页面"
        for i in range(3):
            try:
                eles = self.get_elements(ProductProviderLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(ProductProviderLocator.order_management_tab, doc)
                self.click_element(ProductProviderLocator.order_management_tab, doc)
                self.wait_elevisble(ProductProviderLocator.resource_packages_title_btn, doc)
                self.click_element(ProductProviderLocator.resource_packages_title_btn, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(ProductProviderLocator.resource_packages_page, doc)

    # Resource Packages页面新增provider字段
    def resource_packages_add_provider(self):
        time.sleep(2)
        doc = "Resource Packages页面新增provider字段"
        # 点击reset按钮获取数据
        self.move_mouse_to_click_element(ProductProviderLocator.resource_packages_reset_button, doc)
        time.sleep(10)
        # 页面有产品名称提供者弹窗---可能没数据
        self.move_mouse_to_element(ProductProviderLocator.resource_packages_product_name)
        time.sleep(1)
        self.check_attribute(self.get_element(ProductProviderLocator.resource_packages_product_box, doc).text, 'Provider: tpartner')
        # # 点击order id 进入detail页面--无数据
        # self.move_mouse_to_click_element(ProductProviderLocator.orders_product_order_id, doc)
        # time.sleep(2)
        # self.wait_elevisble(ProductProviderLocator.orders_detail_page, doc)
        # time.sleep(1)
        # self.check_attribute(self.get_element(ProductProviderLocator.orders_detail_provider_title, doc).text, 'Provider')


    # 进入Billing Statement界面
    def enter_billing_statement_pages(self):
        time.sleep(2)
        doc = "进入Billing Statement界面"
        for i in range(3):
            try:
                eles = self.get_elements(ProductProviderLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(ProductProviderLocator.bills_tab, doc)
                self.click_element(ProductProviderLocator.bills_tab, doc)
                self.wait_elevisble(ProductProviderLocator.bills_billing_statement_title_btn, doc)
                self.click_element(ProductProviderLocator.bills_billing_statement_title_btn, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(ProductProviderLocator.bills_billing_statement_page, doc)

    # 进入Billing Statement页面查看月账单信息
    def check_billing_statement_pages(self):
        time.sleep(2)
        doc = "进入Billing Statement页面查看月账单信息"
        # 页面提示语信息
        self.check_attribute(self.get_element(ProductProviderLocator.billing_statement_page_tips, doc).text, 'If you settle accounts by topping up, the system may update the outstanding amount after a certain delay.')
        time.sleep(1)
        # 检测月账单各字段
        self.check_attribute(self.get_element(ProductProviderLocator.billing_statement_invoice_no, doc).text, 'Invoice No.')
        self.check_attribute(self.get_element(ProductProviderLocator.billing_statement_billing_cycle, doc).text, 'Billing Cycle')
        self.check_attribute(self.get_element(ProductProviderLocator.billing_statement_account_email, doc).text, 'Account Email')
        self.check_attribute(self.get_element(ProductProviderLocator.billing_statement_status, doc).text, 'Status')
        self.check_attribute(self.get_element(ProductProviderLocator.billing_statement_gross_amount, doc).text, 'Gross Amount (IDR)')
        self.check_attribute(self.get_element(ProductProviderLocator.billing_statement_paid_amount, doc).text, 'Paid Amount (IDR)')
        self.check_attribute(self.get_element(ProductProviderLocator.billing_statement_outstannding_amount, doc).text, 'Outstanding Amount (IDR)')
        self.check_attribute(self.get_element(ProductProviderLocator.billing_statement_issue_date, doc).text, 'Issue Date (UTC+08:00)')


    #进入my resource界面
    def enter_my_resource_page(self):
        doc='进入user端的myresource页面'
        for i in range(3):
            try:
                eles = self.get_elements(ProductProviderLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(ProductProviderLocator.order_management_tab, doc)
                self.click_element(ProductProviderLocator.order_management_tab, doc)
                self.wait_elevisble(ProductProviderLocator.my_resource_title_btn, doc)
                self.click_element(ProductProviderLocator.my_resource_title_btn, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(ProductProviderLocator.my_resource_page, doc)

    # 查看My Resource页面产品provider属性
    def check_my_resource_provider(self):
        doc = '查看My Resource页面产品provider属性'
        time.sleep(5)
        self.move_mouse_to_element(ProductProviderLocator.product_list)
        time.sleep(2)
        self.check_attribute(self.get_element(ProductProviderLocator.product_provider, doc).text, 'Provider: Flou cloud')


    # 进入Usage Query界面
    def enter_usage_pages(self):
        time.sleep(2)
        doc = "进入Orders界面"
        for i in range(3):
            try:
                eles = self.get_elements(ProductProviderLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(ProductProviderLocator.order_management_tab, doc)
                self.click_element(ProductProviderLocator.order_management_tab, doc)
                self.wait_elevisble(ProductProviderLocator.usage_query_title_btn, doc)
                self.click_element(ProductProviderLocator.usage_query_title_btn, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(ProductProviderLocator.usage_query_page, doc)

    # Usage Query页面新增provider字段
    def usage_query_add_provider(self):
        time.sleep(2)
        doc = "Usage Query页面新增provider字段"
        self.move_mouse_to_element(ProductProviderLocator.usage_query_product_click_button)
        self.click_element(ProductProviderLocator.usage_query_product_click_button, doc)
        time.sleep(1)
        self.move_mouse_to_click_element(ProductProviderLocator.usage_query_click_allproduct, doc)
        time.sleep(5)
        self.move_mouse_to_element(ProductProviderLocator.usage_query_packages_product_name)
        self.check_attribute(self.get_element(ProductProviderLocator.usage_query_packages_product_box, doc).get_attribute("textContent"), 'Provider: Flou')



#####################################################################################################
# Admin portal
#####################################################################################################

    # ############################################################################################
    # 进入Spending Summary界面
    def enter_admin_bills_pages(self):
        time.sleep(2)
        doc = "进入Admin Spending Summary界面"
        for i in range(3):
            try:
                eles = self.get_elements(ProductProviderLocator.admin_default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(ProductProviderLocator.admin_bills_tab, doc)
                self.click_element(ProductProviderLocator.admin_bills_tab, doc)
                self.wait_elevisble(ProductProviderLocator.admin_bills_spending_summary_title_btn, doc)
                self.click_element(ProductProviderLocator.admin_bills_spending_summary_title_btn, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(ProductProviderLocator.admin_bills_spending_summary_page, doc)

    # admin spending summary页面新增provider弹框
    def check_admin_spending_summary_product_overview(self):
        time.sleep(2)
        date_time = (datetime.date.today()).strftime('%Y%m%d')
        doc = "admin spending summary overview页面新增provider字段"
        self.wait_elevisble(ProductProviderLocator.admin_bills_spending_summary_page, doc)
        self.move_mouse_to_element(ProductProviderLocator.overview_account_email_click_button)
        self.click_element(ProductProviderLocator.overview_account_email_click_button, doc)
        time.sleep(2)
        self.move_mouse_to_element(ProductProviderLocator.overview_account_email_value_Personal)
        self.click_element(ProductProviderLocator.overview_account_email_value_Personal, doc)
        time.sleep(2)
        self.move_mouse_to_click_element(ProductProviderLocator.overview_account_email_value_Personal_input, doc)
        self.send_keys(ProductProviderLocator.overview_account_email_value_Personal_input, "test112@qq.com", doc)
        time.sleep(2)
        self.keyboard_event_enter(ProductProviderLocator.overview_account_email_value_Personal_input, doc)
        self.click_element(ProductProviderLocator.overview_Search, doc)
        time.sleep(2)
        self.move_mouse_to_element(ProductProviderLocator.overview_product)
        self.check_attribute(self.get_element(ProductProviderLocator.overview_product_provider, doc).text, 'Provider: Flou')
        time.sleep(2)
        # 点击下载按钮
        self.move_mouse_to_click_element(ProductProviderLocator.overview_download_button, doc)
        time.sleep(15)
        # 验证文件中新增provider
        overview_file_name = 'CostSummary_'+date_time
        self.check_excel_overview_provider(overview_file_name)
        # detail分页
        self.move_mouse_to_click_element(ProductProviderLocator.admin_spending_summary_detail_tab, doc)
        time.sleep(2)
        self.wait_elevisble(ProductProviderLocator.detail_product, doc)
        self.move_mouse_to_element(ProductProviderLocator.detail_product)
        time.sleep(1)
        self.check_attribute(self.get_element(ProductProviderLocator.detail_product_provider, doc).text, 'Provider: Flou')
        time.sleep(2)
        # 点击下载按钮
        self.move_mouse_to_click_element(ProductProviderLocator.admin_detail_download_button, doc)
        time.sleep(15)
        overview_file_name = 'ProductBillingCycle_'+date_time
        self.check_excel_detail_provider(overview_file_name)


    # ############################################################################################
    # 进入Orders界面
    def enter_admin_orders_pages(self):
        time.sleep(2)
        doc = "进入Admin Spending Summary界面"
        for i in range(3):
            try:
                eles = self.get_elements(ProductProviderLocator.admin_default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(ProductProviderLocator.admin_orders_tab, doc)
                self.click_element(ProductProviderLocator.admin_orders_tab, doc)
                self.wait_elevisble(ProductProviderLocator.admin_orders_title_btn, doc)
                self.click_element(ProductProviderLocator.admin_orders_title_btn, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(ProductProviderLocator.admin_orders_page, doc)

    # admin spending summary页面新增provider弹框
    def check_admin_orders_product_detail(self):
        time.sleep(2)
        doc = "admin spending summary页面新增provider字段"
        self.wait_elevisble(ProductProviderLocator.admin_orders_page, doc)
        self.move_mouse_to_element(ProductProviderLocator.orders_product)
        self.check_attribute(self.get_element(ProductProviderLocator.orders_product_provider, doc).text, 'Provider: Flou cloud')
        time.sleep(2)

    # ############################################################################################
    # 进入My Resource界面
    def enter_admin_my_resource_pages(self):
        time.sleep(2)
        doc = "进入Admin Spending Summary界面"
        for i in range(3):
            try:
                eles = self.get_elements(ProductProviderLocator.admin_default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(ProductProviderLocator.admin_my_resource_tab, doc)
                self.click_element(ProductProviderLocator.admin_my_resource_tab, doc)
                self.wait_elevisble(ProductProviderLocator.admin_my_resource_title_btn, doc)
                self.click_element(ProductProviderLocator.admin_my_resource_title_btn, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(ProductProviderLocator.admin_orders_page, doc)

    # admin spending summary页面新增provider弹框
    def check_admin_my_resource_product_detail(self):
        time.sleep(2)
        doc = "admin spending summary页面新增provider字段"
        self.wait_elevisble(ProductProviderLocator.admin_my_resource_page, doc)
        self.move_mouse_to_element(ProductProviderLocator.my_resource_product)
        self.check_attribute(self.get_element(ProductProviderLocator.my_resource_product_provider, doc).text, 'Provider: Flou cloud')
        time.sleep(2)
